package a4_p04_jw_kw.junit;

import static org.junit.Assert.*;

import java.util.ArrayList;

import org.jgrapht.Graph;
import org.jgrapht.graph.DefaultWeightedEdge;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import a1_p04_jw_kw.struct.Node;
import a4_p04_jw_kw.euler.Fleury;
import a4_p04_jw_kw.euler.Hierholzer;
import a4_p04_jw_kw.graph.GraphEUBIGCreater;

public class FleuryHierholzerTest {

	GraphEUBIGCreater bigHierholzer;
	GraphEUBIGCreater bigFleury;

	Graph<Node, DefaultWeightedEdge> gHolz;
	Graph<Node, DefaultWeightedEdge> gFleury;

	public FleuryHierholzerTest() {
	}

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
		createBIGFleury();
		createBIGHierholzer();
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testFleury(){
		Fleury fleury = new Fleury(gFleury);
		fleury.createEulerTour();

		int vertNum = bigFleury.getVertexCount();
		int edgeNum = bigFleury.getEdgeCount();
		int edgeSum = vertNum * edgeNum;
		
		assertTrue("fleury edges count should be equal", edgeSum == fleury.getEulerTour().size());
		
		ArrayList<DefaultWeightedEdge> eulerTour = fleury.getEulerTour();
		int last = eulerTour.size();
		DefaultWeightedEdge firstEdge = eulerTour.get(0);
		DefaultWeightedEdge lastEdge = eulerTour.get(last-1);
		
		Node fNode1 = gFleury.getEdgeSource(firstEdge);
		Node fNode2 = gFleury.getEdgeTarget(firstEdge);
		
		Node lNode1 = gFleury.getEdgeSource(lastEdge);
		Node lNode2 = gFleury.getEdgeTarget(lastEdge);
		
		boolean fBool = fNode1.equals(lNode1) || fNode1.equals(lNode2);
		boolean lBool = fNode2.equals(lNode1) || fNode1.equals(lNode2);
		
		assertTrue("one should be equal", fBool || lBool );
		
	}
	
	@Test
	public void testHierholzer(){
		Hierholzer hierholzer = new Hierholzer();
		hierholzer.createEulerTour("1", gHolz);
		
		int vertNum = bigHierholzer.getVertexCount();
		int edgeNum = bigHierholzer.getEdgeCount();
		int edgeSum = vertNum * edgeNum;
		
		assertTrue("hierholz edges count should be equal", edgeSum == hierholzer.getEulerTour().size());
		
		ArrayList<DefaultWeightedEdge> eulerTour = hierholzer.getEulerTour();
		int last = eulerTour.size();
		DefaultWeightedEdge firstEdge = eulerTour.get(0);
		DefaultWeightedEdge lastEdge = eulerTour.get(last-1);
		
		Node fNode1 = gHolz.getEdgeSource(firstEdge);
		Node fNode2 = gHolz.getEdgeTarget(firstEdge);
		
		Node lNode1 = gHolz.getEdgeSource(lastEdge);
		Node lNode2 = gHolz.getEdgeTarget(lastEdge);
		
		boolean fBool = fNode1.equals(lNode1) || fNode1.equals(lNode2);
		boolean lBool = fNode2.equals(lNode1) || fNode1.equals(lNode2);
		
		assertTrue("one should be equal", fBool || lBool );
	}

	private void createBIGFleury() {
		bigFleury = new GraphEUBIGCreater();
		gFleury = bigFleury.getGraph();
	}

	private void createBIGHierholzer() {
		bigHierholzer = new GraphEUBIGCreater();
		gHolz = bigHierholzer.getGraph();
	}

}
